package ee.subjects.frontend.controller;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ee.subjects.frontend.controller.command.CommandFactory;



/**
 * Servlet implementation class FrontController
 */
@WebServlet("/FrontController")
public class FrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FrontController() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		ServletContext context = getServletConfig().getServletContext();
		
		try{
			
			CommandFactory commandFactory  = new CommandFactory(request,response);
			commandFactory.PrepareCommandsExecution();
			commandFactory.doExecution();
			
			ViewManager ViewManager = 
					new ViewManager(commandFactory.getMode(), commandFactory.getEvent(), request, response, context);
			ViewManager.navigate();
			
			
			
		}catch(Exception ex){  
			//TODO Logimine
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
